#!/bin/bash -e

# Usage: ./run-test-ci [tests...]
#
# Examples:
#
#    ./run-test-ci              # Run all tests
#    ./run-test-ci page conf    # Run only 'test_page' and 'test_conf'
#
BASE=$(dirname $0)

# 'RUNNER' is the binary that the scripts are executed by. It defaults to
# 'python2' if not set in the environment.
# Other examples:
#       PATH=$PWD:$PATH RUNNER='python2 -m coverage run --include=$PWD/**/*' ./run-test
#       PATH=$PWD:$PATH RUNNER='python3' ./run-test
# See ./run-coverage for a real example.
RUNNER="${RUNNER:-python2}"

test_page() {
    set -x
    $RUNNER $BASE/ocropus-nlbin "$BASE/tests/testpage.png" -o temp
    $RUNNER $BASE/ocropus-gpageseg 'temp/????.bin.png' --gray
    $RUNNER $BASE/ocropus-rpred --parallel=0 --nocheck  'temp/0001/01000?.bin.png'
    $RUNNER $BASE/ocropus-dewarp 'temp/0001/01001?.bin.png'
    $RUNNER $BASE/ocropus-hocr 'temp/????.bin.png' -o temp.html 2>/dev/null
    $RUNNER $BASE/ocropus-visualize-results temp
    $RUNNER $BASE/ocropus-gtedit html temp/????/??????.bin.png -o temp-correction.html
    $RUNNER $BASE/ocropus-gtedit extract temp-correction.html
    set +x
}

test_conf() {
    local TESTIMAGE=0079-01000d
    cp "$BASE/tests/$TESTIMAGE"* temp
    set -x
    $RUNNER $BASE/ocropus-rpred temp/$TESTIMAGE.png
    # test different parameters of ocropus-errs
    $RUNNER $BASE/ocropus-errs  temp/$TESTIMAGE.gt.txt
    $RUNNER $BASE/ocropus-errs  temp/$TESTIMAGE.gt.txt --erroronly
    # test different parameters of ocropus-econf
    $RUNNER $BASE/ocropus-econf temp/$TESTIMAGE.gt.txt
    $RUNNER $BASE/ocropus-econf temp/$TESTIMAGE.gt.txt --perfile temp/$TESTIMAGE.perfile.econf.txt
    $RUNNER $BASE/ocropus-econf temp/$TESTIMAGE.gt.txt --allconf temp/$TESTIMAGE.allconf.econf.txt
    # test with empty gt file, no txt file
    echo '' > temp/econf-file.gt.txt
    $RUNNER $BASE/ocropus-econf temp/econf-file.gt.txt
    # test with empty gt file, existing txt file
    echo '0' > temp/econf-file.txt
    $RUNNER $BASE/ocropus-econf temp/econf-file.gt.txt
    # test on files withouth "gt" in their file endings
    $RUNNER $BASE/ocropus-errs  temp/$TESTIMAGE.txt
    $RUNNER $BASE/ocropus-econf temp/$TESTIMAGE.txt
    # test on files where there is no corresponding txt file
    cp temp/$TESTIMAGE.gt.txt temp/$TESTIMAGE.copy.gt.txt
    $RUNNER $BASE/ocropus-errs  temp/$TESTIMAGE.copy.gt.txt
    $RUNNER $BASE/ocropus-econf  temp/$TESTIMAGE.copy.gt.txt
}

test_linegen() {
    $RUNNER $BASE/ocropus-linegen -m 3 -t $BASE/tests/tomsawyer.txt -f $BASE/tests/DejaVuSans.ttf
    $RUNNER $BASE/ocropus-linegen -m 3 -t $BASE/tests/tomsawyer.txt -f $BASE/tests/DejaVuSans.ttf --degradations med --numdir
    $RUNNER $BASE/ocropus-linegen -m 3 -t $BASE/tests/tomsawyer.txt -f $BASE/tests/DejaVuSans.ttf --degradations hi --sizes 40,50,60,70
}

test_rtrain() {
    tar -zxf $BASE/tests/uw3-500.tgz
    $RUNNER $BASE/ocropus-rtrain 'book/*/*.bin.png' -N 5 -o ci-test-model
}

test_rtrain_files() {
    tar -zxf $BASE/tests/uw3-500.tgz
    find 'book' -name '*.bin.png' > INPUT_FILES
    $RUNNER $BASE/ocropus-rtrain -f INPUT_FILES -N 5 -o ci-test-model
    rm INPUT_FILES
}

test_nlbin() {
    local TESTIMAGE=0071-010012.png
    cp $BASE/tests/$TESTIMAGE temp
    $RUNNER $BASE/ocropus-nlbin temp/$TESTIMAGE
    $RUNNER $BASE/ocropus-nlbin temp/$TESTIMAGE -n
    $RUNNER $BASE/ocropus-nlbin temp/$TESTIMAGE -n --gray
}

test_gpageseg() {
    local TESTIMAGE=text-near-edge.bin.png
    cp $BASE/tests/$TESTIMAGE temp
    $RUNNER $BASE/ocropus-gpageseg temp/$TESTIMAGE
    $RUNNER $BASE/ocropus-gpageseg temp/$TESTIMAGE -n
    $RUNNER $BASE/ocropus-gpageseg temp/$TESTIMAGE -n --maxseps 3
    $RUNNER $BASE/ocropus-gpageseg temp/$TESTIMAGE -n -b
    $RUNNER $BASE/ocropus-gpageseg temp/$TESTIMAGE -n --usegauss
    cp $BASE/tests/table.bin.png $BASE/tests/table.mask.png temp
    $RUNNER $BASE/ocropus-gpageseg temp/table.bin.png --debug -n --minscale 7 --maxcolseps 0
}

test_rpred() {
    local TESTIMAGE=0079-01000d
    cp "$BASE/tests/$TESTIMAGE"* temp
    $RUNNER $BASE/ocropus-rpred temp/$TESTIMAGE.png --llocs --alocs --probabilities
    $RUNNER $BASE/ocropus-rpred temp/$TESTIMAGE.png --estrate
}

test_gtedit() {
    set -x
    mkdir -p temp/9999
    echo '0' > temp/9999/000000.gt.txt
    echo 'basel' > temp/9999/000001.gt.txt
    echo '2' > temp/9999/000002.gt.txt
    $RUNNER $BASE/ocropus-gtedit text  temp/9999/*.gt.txt -o temp/gtedit-together.gt.txt
    $RUNNER $BASE/ocropus-gtedit org   temp/9999/*.gt.txt -o temp/gtedit-together.org.gt.txt
    mkdir -p temp/out
    $RUNNER $BASE/ocropus-gtedit write temp/gtedit-together.gt.txt temp/out -x .copy.gt.txt
}

rm -rf temp
mkdir -p temp
if (( $# > 0 ));then
    for test in "$@";do
        test_$test
    done
else
    test_page
    test_conf
    test_linegen
    test_rtrain
    test_rtrain_files
    test_nlbin
    test_gpageseg
    test_rpred
    test_gtedit
fi
